#include "lv_app_timer.h"

static struct
{
  float temp;
  float rh;
  uint32_t smoke;
  uint32_t fire;
} lv_stm32_data;

static void lv_app_timer_update_data_cb(lv_timer_t *timer);

void lv_app_timer_init(void)
{
  lv_timer_t *lv_stm32_data_timer;

  lv_stm32_data_timer = lv_timer_create(lv_app_timer_update_data_cb, 800, NULL);
  lv_timer_set_repeat_count(lv_stm32_data_timer, -1);
}

static void lv_app_timer_update_data_cb(lv_timer_t *timer)
{
  if(HAL_OK==HAL_ADC_PollForConversion(&hadc1,1))
  {
    lv_stm32_data.smoke = HAL_ADC_GetValue(&hadc1);
  }
  HAL_ADC_Start(&hadc1);

  // HAL_ADC_Start(&hadc1);
  // while (__HAL_ADC_GET_FLAG(&hadc1, ADC_FLAG_EOC) == DISABLE);
  // lv_stm32_data.fire = HAL_ADC_GetValue(&hadc1);

  lv_app_screen_1_set_small_box_text_fmt(screen_1_box_smoke, "%d", lv_stm32_data.smoke);
  // lv_app_screen_1_set_small_box_text_fmt(screen_1_box_fire, "%d", lv_stm32_data.fire);

  if (!aht30_readdata(&lv_stm32_data.rh, &lv_stm32_data.temp)) return;
  lv_app_screen_1_set_small_box_text_fmt(screen_1_box_temp, "%2.1f°C", lv_stm32_data.temp);
  lv_app_screen_1_set_small_box_text_fmt(screen_1_box_rh, "%2.1fRH", lv_stm32_data.rh);
}
